﻿using System;
using System.ComponentModel;
using System.Windows.Forms;
using MECPSDB.EntityClasses;
using MECPSDB.FactoryClasses;
using MECPSDB.HelperClasses;
using MECUITMPLTLIB;
using MECPSDB.DatabaseSpecific;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace MEC.WMS.FileMaintenance.Material {
    
    public partial class AeFrm:AeFrmUiTemplate{
        
        private const string Caption = "Material Maintenance";
        
        public AeFrm(string cs, string branchcode, string usercode){
            if (cs == null){
                throw new ArgumentNullException("cs");
            }
            Cs= cs;
            Branchcode = branchcode;
            Usercode = usercode;
            InitializeComponent();
            this.Text = Caption;
        }

        public override sealed string Text{
            get{
                return base.Text;
            }
            set{
                base.Text = value;
            }
        }

        public override void MethodNewEntityProcedure(){
           base.MethodNewEntityProcedure();
           VwMaterialEntity entity = new VwMaterialEntity(); //-implementation
           //todo Add extra code here
           //... 
           
           MainEntity = entity; //-implementation
           MethodAllowEdit(true); ////-implementation
       }

        protected override void MainEntityBackgroundSaveDoWork(object sender, DoWorkEventArgs e){
            VwMaterialEntity vwentity = (VwMaterialEntity) e.Argument;
            MatimgEntity matimgEntity = null;
            MaterialEntity entitytosave = new MaterialEntity(vwentity.MaterialCode);
            using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(Cs)){
                if (vwentity.IsNew){
                    matimgEntity = new MatimgEntity(vwentity.MaterialCode);
                    entitytosave.IsNew = true;
                } else{
                    entitytosave.IsNew = false;
                }
                entitytosave.MatDescription = vwentity.MatDescription;
                entitytosave.BrandCodeFk = vwentity.BrandCodeFk;
                entitytosave.MatGrpCodeFk = vwentity.MatGrpCodeFk;
                entitytosave.UmCodeFk = vwentity.UmCodeFk;
                entitytosave.CritLevel = vwentity.CritLevel;
                entitytosave.ReOrderLevel = vwentity.ReOrderLevel;
                entitytosave.InvLevel = vwentity.InvLevel;

                entitytosave.Remarks = vwentity.Remarks;

                entitytosave.WdserialNo = vwentity.WdserialNo;
                entitytosave.InActive = vwentity.InActive;
                
                dataAccessAdapter.SaveEntity(entitytosave,false);
                if (matimgEntity != null){
                    dataAccessAdapter.SaveEntity(matimgEntity);
                }
                dataAccessAdapter.FetchEntity(vwentity);
                e.Result = vwentity;
            }

        }

        protected override void MethodLoadControlValuesToObjectsForSaving(){
            VwMaterialEntity entity = (VwMaterialEntity) MainEntity;
            entity.MaterialCode = (string) TextEditMaterialCode_ae_pk_.EditValue;
            entity.MatDescription = (string) TextEditDescription_ae_.EditValue;
            entity.Remarks = (string) memoEditRemarks_ae_.EditValue;

            entity.CritLevel = (float) spinEditCritical_ae_.Value;
            entity.ReOrderLevel = (float) spinEditReorder_ae_.Value;
            entity.InvLevel = (float) spinEditInvLevel_ae_.Value;

            entity.InActive = checkEditInActive_ae_.Checked;
            entity.WdserialNo = checkEditWdSerial_ae_.Checked;
            entity.LastModified = DateTime.Now;
            
            entity.BrandCodeFk = (string) lookUpEditBrandCode_ae_.EditValue;
            entity.MatGrpCodeFk = (string) lookUpEditGroupCode_ae_.EditValue;
            entity.UmCodeFk = (string) lookUpEditUomCode_ae_.EditValue;
            
        }

        protected override bool MethodValidate(){
            dxErrorProvider1.ClearErrors();
            bool focused = false;
            if (string.IsNullOrWhiteSpace(TextEditMaterialCode_ae_pk_.Text)){
                dxErrorProvider1.SetError(TextEditMaterialCode_ae_pk_, "Required");
                if (!focused){
                    TextEditMaterialCode_ae_pk_.Focus();
                    focused = true;
                }
            }
            if (string.IsNullOrWhiteSpace(TextEditDescription_ae_.Text)){
                dxErrorProvider1.SetError(TextEditDescription_ae_, "Required");
                if (!focused){
                    TextEditDescription_ae_.Focus();
                    focused = true;
                }
            }
            if (string.IsNullOrWhiteSpace(lookUpEditBrandCode_ae_.Text)){
                dxErrorProvider1.SetError(lookUpEditBrandCode_ae_, "Required");
                if (!focused){
                    lookUpEditBrandCode_ae_.Focus();
                    focused = true;
                }
            }
            if (string.IsNullOrWhiteSpace(lookUpEditGroupCode_ae_.Text)){
                dxErrorProvider1.SetError(lookUpEditGroupCode_ae_, "Required");
                if (!focused){
                    lookUpEditGroupCode_ae_.Focus();
                    focused = true;
                }
            }
            if (string.IsNullOrWhiteSpace(lookUpEditUomCode_ae_.Text)){
                dxErrorProvider1.SetError(lookUpEditUomCode_ae_, "Required");
                if (!focused){
                    lookUpEditUomCode_ae_.Focus();
                    focused = true;
                }
            }
            return !dxErrorProvider1.HasErrors;
        }

        protected override void MethodClearForm(){
        //todo
            base.MethodClearForm();
            checkEditInActive_ae_.Checked = false;
            checkEditWdSerial_ae_.Checked = false;
        }
        protected override void MethodDispayMainEntityToControls(){
        //todo
            base.MethodDispayMainEntityToControls();
           VwMaterialEntity entity = (VwMaterialEntity) MainEntity;
            TextEditMaterialCode_ae_pk_.EditValue = entity.MaterialCode;
            TextEditDescription_ae_.EditValue = entity.MatDescription;

            lookUpEditBrandCode_ae_.EditValue = entity.BrandCodeFk;
            lookUpEditGroupCode_ae_.EditValue = entity.MatGrpCodeFk;
            lookUpEditUomCode_ae_.EditValue = entity.UmCodeFk;

            spinEditCritical_ae_.EditValue = entity.CritLevel;
            spinEditInvLevel_ae_.EditValue = entity.InvLevel;
            spinEditReorder_ae_.EditValue = entity.ReOrderLevel;

            checkEditInActive_ae_.EditValue = entity.InActive;
            checkEditWdSerial_ae_.EditValue = entity.InActive;

            memoEditRemarks_ae_.EditValue = entity.Remarks;
        }

        protected override void MainEntityFetchProgressChanged(object sender, ProgressChangedEventArgs e){
            base.MainEntityFetchProgressChanged(sender, e);
            EntityCollection ec = (EntityCollection) e.UserState;
            if (ecbrand.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
                ecbrand = ec;
                lookUpEditBrandCode_ae_.Properties.DataSource = ec;
            }else
            if (ecgroup.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
                ecgroup = ec;
                lookUpEditGroupCode_ae_.Properties.DataSource = ec;
            }else
            if (ecuom.EntityFactoryToUse.GetType() == ec.EntityFactoryToUse.GetType()){
                ecuom = ec;
                lookUpEditUomCode_ae_.Properties.DataSource = ec;
            }           

        }
        protected override void MainEntityBackgroundFetchDoWork(object sender, DoWorkEventArgs e){
            FetchParam fetchParam = (FetchParam) e.Argument;
            BackgroundWorker bw = (BackgroundWorker) sender;
            //TODO LookupFetches
            if (fetchParam.FetchLookupTables){
                EntityCollection ectbrand = new EntityCollection(new MatbrandEntityFactory());
                EntityCollection ectgroup = new EntityCollection(new MatgroupEntityFactory());
                EntityCollection ectuom = new EntityCollection(new MatumEntityFactory());
                using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(fetchParam.Cs)){
                    dataAccessAdapter.FetchEntityCollection(ectbrand, null);
                    bw.ReportProgress(1, ectbrand);
                    dataAccessAdapter.FetchEntityCollection(ectgroup, null);
                    bw.ReportProgress(2, ectgroup);
                    dataAccessAdapter.FetchEntityCollection(ectuom, null);
                    bw.ReportProgress(3, ectuom);
                }
            }

            //TODO MainEntityFetch
            if (fetchParam.FetchMainEntity){
                VwMaterialEntity entity = new VwMaterialEntity(fetchParam.PkCode);
                IPrefetchPath2 prefetchPath = null;
                ExcludeIncludeFieldsList excludedfields = new ExcludeIncludeFieldsList();
                using (DataAccessAdapter dataAccessAdapter = new DataAccessAdapter(fetchParam.Cs)){
                    dataAccessAdapter.FetchEntity(entity, prefetchPath, null, excludedfields);
                    fetchParam.MainEntityFetched = entity;
                    e.Result = fetchParam;
                    return;
                }
            }
        }

        private void LookUpEditBrandCodeAeEditValueChanged(object sender,EventArgs e){
            try{
            if (lookUpEditBrandCode_ae_.EditValue == null){
                textEditbranddesc_ae_ro_.EditValue = null;
                return;}
            MatbrandEntity selected = (MatbrandEntity) (lookUpEditBrandCode_ae_.GetSelectedDataRow());
            textEditbranddesc_ae_ro_.EditValue = selected.BrandDescription;
            } catch (Exception ex){
                MessageBox.Show(ex.Message);
            }
        }
            

        private void LookUpEditGroupCodeAeEditValueChanged(object sender,EventArgs e) {
        try{
            if (lookUpEditGroupCode_ae_.EditValue == null){
                textEditgroupdesc_ae_ro_.EditValue = null;
                return;}
            MatgroupEntity selected = (MatgroupEntity) (lookUpEditGroupCode_ae_.GetSelectedDataRow());
            textEditgroupdesc_ae_ro_.EditValue = selected.MatGrpDescription;
            } catch (Exception ex){
                MessageBox.Show(ex.Message);
            }
        }

        private void LookUpEditUomCodeAeEditValueChanged(object sender,EventArgs e) {
                try{
            if (lookUpEditUomCode_ae_.EditValue == null){
                textEdituomdesc_ae_ro_.EditValue = null;
                return;}
            MatumEntity selected = (MatumEntity) (lookUpEditUomCode_ae_.GetSelectedDataRow());
            textEdituomdesc_ae_ro_.EditValue = selected.UomDescription;
            } catch (Exception ex){
                MessageBox.Show(ex.Message);
            }
        }


        
    }//End class
} // End namespace